package com.just.recruitback.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.just.recruitback.entity.Company;
import com.just.recruitback.entity.Job;
import com.just.recruitback.entity.Zhilian;
import com.just.recruitback.service.base.ICompanyService;
import com.just.recruitback.service.base.IJobService;
import com.just.recruitback.service.base.IZhilianService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : baibaiwucahng
 * @date : 2020/7/2  22:59
 */
@Service
@Slf4j
public class MigrationService {
    @Resource
    private IZhilianService iZhilianService;
    @Resource
    private ICompanyService companyService;
    @Resource
    private IJobService iJobService;

    public void migrationData(int begin, int start) {
       int companyId = 0;
        for (int i = begin; i < start; i++) {
            Page<Zhilian> page = new Page<>(i, 100);
            List<Zhilian> zhilians = iZhilianService.page(page).getRecords();
            List<Job> jobs = new ArrayList<>();
            for (Zhilian zhilian: zhilians) {
                Company company = Company.zhilianConvertToCompany(zhilian);
                QueryWrapper<Company> companyQueryWrapper = new QueryWrapper<>();
                companyQueryWrapper.eq("name", company.getName());
                Company one = companyService.getOne(companyQueryWrapper);
                if (ObjectUtils.isNotNull(one)) {
                    companyId = one.getId();
                } else {
                    companyService.save(company);
                    companyId = company.getId();
                }
                Job job = Job.zhilianConvertToJob(zhilian, companyId);
                jobs.add(job);
                log.error("执行完第{}个100条, 第{}条", i-1, "xxxx");
            }
            iJobService.saveBatch(jobs);
            log.error("执行完第{}个100条", i);
        }
    }
}
